package com.gongxt.data;

import java.util.ArrayList;

import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.Photo;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.util.Log;

public class ContactHelper {
	private static String TAG = "ContactHelper";
	
	private static ContactHelper instance = null;
	private static Context mContext;
	
	private ContactHelper(Context c){
		mContext = c.getApplicationContext();
	}

	public static ContactHelper getInstance(Context context){
		if (instance == null){
			instance = new ContactHelper(context.getApplicationContext());
		}
		
		return instance;
	}
	
	public ArrayList<Long> getStarredMembers() {
		String[] projection = {Contacts._ID};  
		String selection = Contacts.STARRED + " = 1 AND " + Contacts.HAS_PHONE_NUMBER + "= 1";
		Cursor cursor = null;
		ArrayList<Long> arrMember = new ArrayList<Long>();
		try {
			cursor = mContext.getContentResolver().query(Contacts.CONTENT_URI, 
					projection, 
					selection, 
					null, 
					null);
			while (cursor.moveToNext()) {
				arrMember.add(cursor.getLong(0)); // get starred member's phone id
			}
		} catch (Exception e) {
			Log.e(TAG, e.getMessage());
		} finally {
			if (cursor != null && !cursor.isClosed()){
				cursor.close();
			}
		}
		
		return arrMember;
	}
	
	public ContactInfo getContact(long personId) {
		if (personId < 1)
			return null;
		Cursor dataCursor = null;
		Cursor rawCursor = null;
		ContactInfo cInfo = new ContactInfo();
		try {
			// get raw contact
			Uri rawUri = ContentUris.withAppendedId(Contacts.CONTENT_URI,
					personId);
			String[] projection = { Contacts.STARRED, Contacts._ID};
			//String selection = Contacts.DELETED + " = 0";
			rawCursor = mContext.getContentResolver().query(rawUri, projection, null, null,
					null);
			if (rawCursor.moveToNext()) {
				dataCursor = mContext.getContentResolver().query(ContactsContract.Data.CONTENT_URI,
						null, ContactsContract.Data.CONTACT_ID
								+ " IS NOT NULL " + " AND "
								+ ContactsContract.Data.CONTACT_ID + " = "
								+ Long.toString(personId), 
								null, 
								ContactsContract.Data.RAW_CONTACT_ID);

				while (dataCursor.moveToNext()) {
					String mimeType = dataCursor.getString(dataCursor.getColumnIndex(ContactsContract.Data.MIMETYPE));
					
					// name
					if (mimeType.equals(StructuredName.CONTENT_ITEM_TYPE)) {
						cInfo.setLastName(dataCursor.getString(dataCursor
								.getColumnIndex(StructuredName.FAMILY_NAME)));
						cInfo.setFirstName(dataCursor.getString(dataCursor
								.getColumnIndex(StructuredName.GIVEN_NAME)));
						cInfo.setMiddleName(dataCursor.getString(dataCursor
								.getColumnIndex(StructuredName.MIDDLE_NAME)));
						cInfo.setDisplayName(dataCursor.getString(dataCursor
								.getColumnIndex(StructuredName.DISPLAY_NAME)));
						
					// image
					}else if (mimeType.equals(Photo.CONTENT_ITEM_TYPE)){
						cInfo.setImage(dataCursor.getBlob(dataCursor
								.getColumnIndex(Photo.PHOTO)));
						Log.d(TAG, cInfo.getImage() == null ? "null" : "the photo length"+cInfo.getImage().length);
					
					}else if (mimeType.equals(Phone.CONTENT_ITEM_TYPE)) {
						if (dataCursor.getInt(dataCursor.getColumnIndex(ContactsContract.Data.IS_PRIMARY)) > 0){
							cInfo.setPrimaryPhone(dataCursor.getString(dataCursor.getColumnIndex(Phone.NUMBER)));
						}
					}
				}
				
				cInfo.setPhoneCid(personId);

			}
		} catch (Exception e) {
			Log.e(TAG, e.getMessage());
		} finally {
			if (dataCursor != null && !dataCursor.isClosed())
				dataCursor.close();
			if (rawCursor != null && !rawCursor.isClosed())
				rawCursor.close();
		}

		return cInfo;
	}	
}
